﻿var eventId;
$(function () {
    ajaxCall('../DataService.asmx/GetAllEvents', false, "",
                function (msg) {
                    if (msg.d == null) {
                        $('#spanInfo').html("There are no events in the database! Create new event!");
                    }
                    else {
                        for (i in msg.d) {
                            var btnColor = 'white';
                            var btnFinColor = 'white';
                            if (msg.d[i].Active == '1') {
                                btnColor = 'red';
                            }
                            if (msg.d[i].Finished == '1') {
                                btnFinColor = 'green';
                            }
                            var events = [
                            { ID: msg.d[i].EventID, eventName: msg.d[i].EventName, startsAt: JSONDateWithTime(msg.d[i].StartingFrom), endsAt: JSONDateWithTime(msg.d[i].EndingAt), location: msg.d[i].Location, color: btnColor, fincolor: btnFinColor }
                        ];
                            $('#EventsTemplate').tmpl(events).appendTo('#eventContent');
                        }
                    }
                },
                function () {
                    alert("Error happend while reading from database");
                }
            );
});

function DeleteEvent(id) {
    if (confirm("Are you sure you want to delete this item?")) {
        var data = '{"eventId":"' + id + '"}';
        ajaxCall('../DataService.asmx/DeleteEvent', false, data,
                    function () {
                        alert("Item deleted successfully");
                        document.location.reload(true);
                    },
                    function () {
                        alert("Error happend while deleting item!");
                    }
                );
    }
            }

function MakeCurrent(id) {
    ajaxCall('../DataService.asmx/SetEventToCurrent', false, '{"eventId" : "' + id + '"}',
        function() {
            document.location.reload(true);
        },
        function() {
            alert("Error happend while accessing the database");
        }
    );
}

function SetToFinished(id) {
    ajaxCall('../DataService.asmx/SetEventToFinished', false, '{"eventId" : "' + id + '"}',
        function () {
            document.location.reload(true);
        },
        function () {
            alert("Error happend while accessing the database");
        }

    );            
}

function ShowImpressionsDialog(id) {
    eventId = id;
    $('#VisitorImpressionsDialog').dialog({
       width:500,
       modal:true,
       closeOnEscape:false
    });
}

function InsertImpressions() {
    $('#validateImpressionsPages').validate({
        rules: {
            keynote: "required",
            breaks: "required"
        },
        messages: {
            keynote: "This is a required field",
            breaks: "This is a required field"
        },
        errorPlacement: function (error, element) {
            error.insertBefore(element);
        }
    });

    if ($('#txtKeynoteLink').valid() && $('#txtVisitorImpressions').valid()) {
        ajaxCall("../DataService.asmx/UpdateTimeSlots", false, "{'eventId':'" + eventId + "', 'keynoteLink':'" + $('#txtKeynoteLink').val() + "', 'breakLink':'" + $('#txtVisitorImpressions').val() + "' }",
            function () {
                alert("Update was successful!");
                $('#VisitorImpressionsDialog').dialog("close");
            },
            function () {
                alert("Error happend while performing this update");
            }
        );
    }

}
